﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Frame.Lib.Core.Log.Message;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Common;

namespace Frame.Web.Admin.Log
{
    public partial class MessageList : PageList
    {
        private LogMessageBO Log
        {
            get
            {
                return LogMessageBO.Instance<LogMessageBO>();
            }
        }

        private string Tag
        {
            get
            {
                return Request.QueryString["Tag"];
            }
        }
        private string StartTime
        {
            get
            {
                return Request.QueryString["StartTime"];
            }

        }

        private string EndTime
        {
            get
            {
                return Request.QueryString["EndTime"];
            }
        }

        private string PageSize
        {
            get
            {
                return Request.QueryString["PageSize"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Pager.BindDrp(drpPager);

                if (!string.IsNullOrEmpty(this.PageSize))
                {
                    drpPager.SelectedValue = this.PageSize;
                }
                if (!string.IsNullOrEmpty(this.Tag))
                {
                    txtTag.Text = this.Tag;
                }
                if (!string.IsNullOrEmpty(this.StartTime))
                {
                    txtStartTime.Text = this.StartTime;
                }
                if (!string.IsNullOrEmpty(this.EndTime))
                {
                    txtEndTime.Text = this.EndTime;
                }

                //_setting.SortExpression = "InputTime";
                //_setting.SortDirection = Jzy.Core.Common.SortDirection.desc;
                BindView();

                //CustSort.SwapSortDirection(_setting);//必须要在绑定之后
            }
            InitSelectID(hidSelectIDs);
            JSUtil.SetTableHeight("list", 145);
            AddAttr();
        }

        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "消息日志记录");
            JSUtil.ClickBtn(lnkDelete, btnDelete);


            WebUtil.SetTimeBox(txtStartTime, false);
            WebUtil.SetTimeBox(txtEndTime, false);
            btnDelete2.Attributes.Add("onclick", "return confirm('确认删除消息日志记录?')");
            btnClear.Attributes.Add("onclick", "return confirm('确认删除全部消息日志记录?')");


            JSUtil.ClickBtn(lnkClear, btnClear);


        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            chkSelectAll.Checked = false;

            
            //GVUtil.InitGV(gvList);
            //Pager.SetPageSize(_setting, drpPager);
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = int.Parse(drpPager.SelectedValue);

            setting.OrderBy = "InputTime desc";

            if (txtTag.Text != "")
            {
                setting.Where = "Tag = @p0";
            }
            if (txtStartTime.Text != "")
            {
                if (string.IsNullOrEmpty(setting.Where))
                {
                    setting.Where += " InputTime >= @p1";
                }
                else
                {
                    setting.Where += " and InputTime >= @p1";
                }
            }
            if (txtEndTime.Text != "")
            {
                if (string.IsNullOrEmpty(setting.Where))
                {
                    setting.Where += "InputTime <= @p2";
                }
                else
                {
                    setting.Where += " and InputTime <= @p2";
                }
            }

            IEnumerable<LogMessageInfo> list = this.Log.GetList(setting, txtTag.Text,txtStartTime.Text.Trim(),txtEndTime.Text.Trim());

            // setting.RecordCount = 80;
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();

            string prefix = GlobalVar.WebCurrPath + "MessageList.aspx?PageSize=" + drpPager.SelectedValue + "&Tag=" + Server.UrlEncode(txtTag.Text) + "&StartTime=" + txtStartTime.Text + "&EndTime=" + txtEndTime.Text;


            litPager.Text = HtmlPager.Render(setting, prefix, true);
            // _sorter.SetInitSort();//初始化排序字段

        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                this.Log.Delete(StrUtil.SplitToStr(strSelectIDs));
                BindView();
            }
        }
        protected void drpPager_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.Log.Delete();
            BindView();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindView();
        }

        protected void btnDelete2_Click(object sender, EventArgs e)
        {
            this.Log.Delete(txtStartTime.Text, txtEndTime.Text);
            BindView();
        }
    }
}